package com.maxlen.order.controller.reception;

import com.alipay.api.AlipayApiException;
import com.maxlen.common.utils.R;
import com.maxlen.common.utils.ResultCode;
import com.maxlen.order.entity.dto.OrderSubmitDTO;
import com.maxlen.order.entity.vo.OrderAllVO;
import com.maxlen.order.exception.NotStockException;
import com.maxlen.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/order/reception")
public class OrderWebController {

    @Autowired
    private OrderService orderService;

    /**
     * 更新订单状态为已删除
     * @param orderSn 订单号
     * @return
     */
    @DeleteMapping("/order/{orderSn}")
    public R deleteOrder(@PathVariable String orderSn){
        return orderService.deleteOrder(orderSn);
    }

    /**
     * 获取某个用户的不同状态的订单
     * @return
     */
    @GetMapping("/getOrder/{status}")
    public R getOrder(@PathVariable(value = "status",required = false) Integer status) throws ExecutionException, InterruptedException {
        OrderAllVO orderAllVO = orderService.getOrder(status);
        return R.ok().setData(orderAllVO);
    }

    /**
     * 获取某个用户的所有订单
     * @return
     */
    @GetMapping("/getOrder")
    public R getOrder() throws ExecutionException, InterruptedException {
        OrderAllVO orderAllVO = orderService.getOrder(null);
        return R.ok().setData(orderAllVO);
    }

    /**
     * 支付宝付款
     * @param orderSn
     * @return
     */
    @GetMapping("/ZFBPay/{orderSn}")
    public R ZFBPay(@PathVariable("orderSn") String orderSn) throws AlipayApiException {
        return orderService.ZFBPay(orderSn);
    }

    /**
     * 获取订单的过期时间
     * @param orderSn
     * @return
     */
    @GetMapping("/getOrderTimeByOrderSn/{orderSn}")
    public R getOrderTime(@PathVariable("orderSn") String orderSn){
        return orderService.getOrderTimeByOrderSn(orderSn);
    }

    /**
     * 获取所有选中的商品的信息
     * @return
     */
    @GetMapping("/getOrderConfirm")
    public R getOrderConfirm(){
        return orderService.confirmOrder();
    }

    /**
     * 生成订单
     * @param orderSubmitDTO
     * @return
     */
    @PostMapping("/setSubmitOrder")
    public R setSubmitOrder(@RequestBody OrderSubmitDTO orderSubmitDTO){
        try{
            return orderService.setSubmitOrder(orderSubmitDTO);
        }catch (NotStockException e){
            return R.error(ResultCode.NO_STOCK.getCode(),e.getMessage() + ResultCode.NO_STOCK.getMessage());
        }
    }

}
